// 代码题 一、
const promise = new Promise((resolve) => {
  const a = 'hello';
  resolve(a);
});
promise.then((value) => {
  const b = 'lagou';
  return `${value} ${b}`;
}).then((value) => {
  const c = 'I ❤ U';
  return `${value} ${c}`;
}).then((value) => {
  console.log(value);
});


// 代码题 二、
const fp = require('lodash/fp');
const cars = [
  {name: 'Ferrari FF', horsepower: 660, dollar_value: 700000, is_stock: true},
  {name: 'Spyker C12 zagato', horsepower: 650, dollar_value: 648000, is_stock: false},
  {name: 'Jaguar XXR-S', horsepower: 550, dollar_value: 132000, is_stock: false},
];
// 练习一
const getIsStock = (car) => {
  return car.is_stock;
};
const getLastInStock = fp.flowRight(getIsStock, fp.last);
console.log('最后一辆车的is_stock为：', getLastInStock(cars));

// 练习二
const getFirstCarName = fp.flowRight(fp.prop('name'), fp.first);
console.log('第一辆车的name为：', getFirstCarName(cars));


// 练习三、 待优化
const _average = function (xs) {
  return fp.reduce(fp.add, 0, xs) / xs.length;
};
const averageDollarValue = fp.flowRight(_average, fp.map(fp.prop('dollar_value')));
console.log(averageDollarValue(cars));

// 练习四、
let _underscore = fp.replace(/\W+/g, '_');
const arr = ['HELLO World'];
const sanitizeNames = fp.flowRight(fp.map(fp.flowRight(_underscore, fp.toLower)));
const result = sanitizeNames(arr);
console.log('result: ', result);


// 三
// 练习1.
const ex1 = (arr) => {
  const result = [];
  for (const item of arr) {
    result.push(item + 1);
  }
  return result;
};
const {Container, Maybe} = require('./support');
const r = Maybe.of([5, 6, 1]).map(x => ex1(x));
console.log(r);


// 练习2.
const ex2 = (arr) => {
  return fp.first(arr);
};
const xs = Container.of(['do', 'ray', 'me', 'fa', 'so', 'la', 'ti', 'do']).map(x => ex2(x));
console.log(xs);


// 练习3.
const safeProp = fp.curry((x, o) => Maybe.of(o[x]));
const user = {id: 2, name: 'Albert'};
const ex3 = () => {
  const firstLetter = fp.first(safeProp('name')(user)._value);
  console.log('第一个字母是：',firstLetter);
  return firstLetter;
};
ex3();

// 练习4.
const ex4 = (num)=>{
  const result = Maybe.of(num).map(x=>parseInt(x));
  console.log(result._value);
  return result._value;
};
ex4(null);
